package com.szsh.aiot.hsm.service.impl;

import com.google.gson.Gson;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.utils.TimeTools;
import com.szsh.aiot.hsm.domain.IDeviceGoonline;
import com.szsh.aiot.hsm.domain.deviceonline.DeviceOnlineModel;
import com.szsh.aiot.hsm.mapper.IDeviceGoonlineMapper;
import com.szsh.aiot.hsm.service.DeviceSatusService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Date 2023/10/24: 16:05
 * @Author: LOADING>>>>>
 */

@Service
public class DeviceSatusServiceImpl implements DeviceSatusService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceSatusServiceImpl.class);

    @Autowired
    private IDeviceGoonlineMapper goonlineMapper;


    @Override
    public AjaxResult getDeviceOnline(String broadbandNO) {

        List<DeviceOnlineModel> modelList = new ArrayList<>();
        //根据账号查询3天内上线的新设备记录
        String dateStr = TimeTools.date2String(TimeTools.plusDays(-3),"yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isBlank(broadbandNO)){
            return AjaxResult.error("请输入正确的宽带号码！");
        }
        IDeviceGoonline iDeviceGoonline = new IDeviceGoonline();
        iDeviceGoonline.setBroadbandNO(broadbandNO);
        iDeviceGoonline.setReceiveTime(TimeTools.plusDays(-3));
        List<IDeviceGoonline> onLineList = goonlineMapper.selectIDeviceGoonlineList(iDeviceGoonline);

        LOGGER.info("onLineList{}",new Gson().toJson(onLineList));

        onLineList.stream().forEach(t->{
            DeviceOnlineModel model = new DeviceOnlineModel();
            model.setTime(TimeTools.date2String(t.getReceiveTime(),"yyyy-MM-dd HH:mm:ss"));
            model.setDeviceName(t.getShowName());
            model.setDeviceMac(t.getDeviceMac());
            model.setNewFlag(false);
            modelList.add(model);
        });
        return new AjaxResult(AjaxResult.Type.SUCCESS,"查询成功",modelList);
    }
}
